#include"SeqList.h"
#include"Contact.h"
void ContactInit(contact * pcon)
{
    SLInit(pcon);
}
void ContactDestroy(contact * pcon)
{
    SLDestroy(pcon);
}
//添加联系人
void ContactAdd(contact* pcon)
{
    CInfo info;
    printf ("请输入联系人姓名：\n");
    scanf ("%s",info.name);
    printf ("请输入联系人姓别：\n");
    scanf ("%s",info.sex);
    printf ("请输入联系人年龄：\n");
    scanf ("%d",&info.age);
    printf ("请输入联系人电话：\n");
    scanf ("%s",info.tel);
    printf ("请输入联系人地址：\n");
    scanf ("%s",info.addr);
    //往通讯录（顺序表）中插入数据
    SLPushBack(pcon,info);
}

//通过姓名查找联系人
int FindByName (contact* pcon,char name[])
{
    for (int i=0;i<pcon->size;i++)
    {
        if(strcmp(pcon->a[i].name,name)==0)
            return i;
    }
    return -1;
}

//删除联系人
void ContactDel(contact* pcon)
{
    //强制要求用姓名查找
    printf ("请输入要删除的联系人姓名：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    int Findidex=FindByName(pcon,name);
    if(Findidex<0)
    {
        printf("查无此人\n");
        return;
    }
    //找到了，删除Findidex处的数据
    SLErase(pcon,Findidex);
}

//修改联系人
void ContactModify(contact* pcon)
{
    printf ("请输入要修改的联系人姓名：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    int find=FindByName(pcon,name);
    if(find<0)
    {
        printf("该联系人不存在\n");
        return ;
    }
    printf("请输入新的联系人姓名：\n");
    scanf("%s",pcon->a[find].name);
    printf("请输入新的联系人性别：\n");
    scanf("%s",pcon->a[find].sex);
    printf("请输入新的联系人年龄：\n");
    scanf("%d",&pcon->a[find].age);
    printf("请输入新的联系人电话：\n");
    scanf("%s",pcon->a[find].tel);
    printf("请输入新的联系人住址：\n");
    scanf("%s",pcon->a[find].addr);
    printf("修改成功！\n");
}

//查看通讯录(打印通讯录的所有数据)
void ContactShow(contact* pcon)
{
    //先打印表头
    printf ("%s %s %s %s %s\n","姓名","性别","年龄","电话","住址");
    for(int i=0;i<pcon->size;i++)
    {
        printf (" %-4s %-4s %-4d %-4s %-4s\n",
            pcon->a[i].name,
            pcon->a[i].sex,
            pcon->a[i].age,
            pcon->a[i].tel,
            pcon->a[i].addr);
    }//对齐好难
}

//查找指定联系人
void ContactFind(contact* pcon)
{
    printf ("请输入要查找的联系人：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    int find=FindByName(pcon,name);
    if(find<0)
    {
        printf("该联系人不存在\n");
        return ;
    }
    //打印
    printf ("%s %s %s %s %s\n","姓名","性别","年龄","电话","住址");
    printf (" %-4s %-4s %-4d %-4s %-4s\n",
            pcon->a[find].name,
            pcon->a[find].sex,
            pcon->a[find].age,
            pcon->a[find].tel,
            pcon->a[find].addr);
}